package com.crape.common.thread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;

import com.crape.common.mail.EmailSentParam;
import com.crape.common.mail.EmailService;

public class EmailThreadPool {

    private static ThreadPoolExecutor threadPool = new ThreadPoolExecutor(15, 30, 5, TimeUnit.SECONDS,
                                                                          new ArrayBlockingQueue<Runnable>(8),
                                                                          new ThreadPoolExecutor.DiscardOldestPolicy());
    private static EmailService       emailService;

    @Autowired
    public void setEmailService(EmailService emailService) {
        this.emailService = emailService;
    }

    public void doMailSendWork(EmailSentParam emailSentParam) {
        if (emailSentParam == null) {
            throw new IllegalArgumentException("parmas is null.");
        }
        threadPool.execute(new EmailThread(emailService, emailSentParam));
    }
}
